package com.itheima.demo1.transaction.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import com.itheima.utils.JDBCUtils;
import com.itheima.utils.JDBCUtils2;

public class AccountDao {

	public void outMoney(Connection conn, String from, Double money) throws SQLException {
		QueryRunner queryRunner = new QueryRunner();
		String sql = "update account set money = money - ? where name = ?";
		queryRunner.update(conn, sql, money, from);
	}

	public void inMoney(Connection conn, String to, Double money) throws SQLException {
		QueryRunner queryRunner = new QueryRunner();
		String sql = "update account set money = money + ? where name = ?";
		queryRunner.update(conn, sql, money, to);
	}
	
	/**
	 * DAO扣钱的方法
	 * @param conn2 
	 * @param from
	 * @param money
	 */
	public void outMoney(String from, Double money) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			// 获得连接
			conn = JDBCUtils2.getConnection();
			// 编写SQL
			String sql = "update account set money = money - ? where name = ?";
			// 预编译SQL
			stmt = conn.prepareStatement(sql);
			stmt.setDouble(1, money);
			stmt.setString(2, from);
			stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				//conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * DAO给某个用户加钱的方法
	 * @param to
	 * @param money
	 */
	public void inMoney(String to, Double money) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			// 获得连接
			conn = JDBCUtils2.getConnection();
			// 编写SQL
			String sql = "update account set money = money + ? where name = ?";
			// 预编译SQL
			stmt = conn.prepareStatement(sql);
			stmt.setDouble(1, money);
			stmt.setString(2, to);
			stmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				//conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}


}
